<?php
namespace app\dsadmin\model;
use think\Model;
use think\Db;

class GongyouModel extends Model
{
    protected $name = 'gongyou';

    public function getGongyouByWhere($map, $Nowpage, $limits)
    {
        return $this->where($map)->page($Nowpage, $limits)->order('id desc')->select();
    }

    public function insertGongyou($param)
    {
        
        // die('不要随意操作，片库不一样');
        try
        {
            $date = date("Y-m-d H:i:s");
            if (isset($param['classtype']))
            {
                $for = explode("\n", $param['txt']);
                foreach($for as $vo)
                {
                    $v = explode("|", $vo); //视频地址|标题|主图地址
                     $add[] = array('name' => $v['1'], 'url' => $v['0'], 'type' => $param['type'], 'photo' => $v['2'], 'shijian' => $date,'pianku' => 1,'time' => $v['3']);
                    // $add[] = array('name' => $v['1'], 'url' => $v['0'], 'type' => $param['type'], 'photo' => $v['2'], 'shijian' => $date);
                }
                $result = $this->insertAll($add);
            }
            else
            {
                $param['shijian'] = $date;
                $result = $this->allowField(true)->save($param);
            }
            if (false === $result)
            {
                return ['code' => -1, 'data' => '', 'msg' => $this->getError()];
            }
            else
            {
                return ['code' => 1, 'data' => '', 'msg' => '添加成功'];
            }
        }
        catch(PDOException $e)
        {
            return ['code' => -2, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    public function updateGongyou($param)
    {
        try
        {
            $result = $this->allowField(true)->save($param, ['id' => $param['id']]);
            if (false === $result)
            {
                return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
            }
            else
            {
                Db::name('siyou')->where(['pid' => $param['id']])->update(['name' => $param['name'], 'url' => $param['url'], 'photo' => $param['photo']]);

                return ['code' => 1, 'data' => '', 'msg' => '编辑成功'];
            }
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    public function getOneGongyou($id)
    {
        return $this->where('id', $id)->find();
    }

    public function addsiyou($data)
    {
    	//发布所有公有片库中未发布的视频
    	if($data['id'] == 'all')
    	{
			$siyou = Db::name('siyou')->where(['userid' => session('dailiuid')])->select();
			$allreadyAddId = array();
			for($i=0; $i<count($siyou); $i++)
			{
				$allreadyAddId[] = $siyou[$i]['pid'];
			}
			$gongyou = $this->where(['id' => ['not in', $allreadyAddId]])->select();
    	}
    	else
    	{
    		$gongyou = $this->where(['id' => ['in', $data['id']]])->select();
    	}
        $array = [];
        $i = 0;
        $siyoucount = Db::name('siyou')->where(['userid' => session('dailiuid')])->count();

        if ($siyoucount + 1 > config('dlzdsps'))
        {
            return ['code' => 0, 'data' => '', 'msg' => "视频数超过限制"];
        }

        for($i=0; $i<count($gongyou); $i++)
        {
           $array[$i]['userid'] = session('dailiuid');
           if ($data['isduoge'] == 0)
           {
               $array[$i]['name'] = $data['title'];
           }
           else
           {
               $array[$i]['name'] = $gongyou[$i]['name'];
           }

           if ($data['issuiji'] == '1')
           {
               $array[$i]['money'] = $data['guding'];
               $array[$i]['sj'] = $data['guding'];
           }
           else
           {
               $array[$i]['money'] = $data['end'];
               $array[$i]['sj'] = $data['start'];
           }
           $array[$i]['zykey'] = md5(session('dailiuid') . uniqid() . random(2));

           $array[$i]['userid'] = session('dailiuid');
           $array[$i]['url'] = $gongyou[$i]['url'];
           $array[$i]['photo'] = $gongyou[$i]['photo'];
           $array[$i]['shijian'] = date("Y-m-d H:i:s");
           $array[$i]['pid'] = $gongyou[$i]['id'];
           $array[$i]['type'] = $data['type']?$data['type']:$gongyou[$i]['type'];
        }
        
        $countInsert = 0;
        $limit = 200;
		$count=ceil(count($array)/$limit);
		for ($i=1;$i<=$count;$i++){
			$offset=($i-1)*($limit);
			$ids=array_slice($array,$offset,$limit);
			$countInsert += Db::name('siyou')->insertAll($ids);
		}

		return ['code' => 1, 'data' => '', 'msg' => '发布成功' . $countInsert . '条'];
    
    }

    public function delGongyou($id)
    {
        try
        {
            $this->where(['id' => ['in', $id]])->delete();
            Db::name('siyou')->where(['pid' => ['in', $id]])->delete();
            return ['code' => 1, 'data' => '', 'msg' => '删除成功'];
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }
}
